<?php

    @session_start();
    require_once getenv('DOCUMENT_ROOT')."/lib/config.inc";
	require_once "system.inc"; 
	require_once "mysql.inc"; 
	
	$db = new mysql(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME);

	
	require_once "statistics.inc"; 
	
    if ( isset($_POST['registered']) ) {
    	
        if ( $_POST['testjs'] == 'Y') {
        	$_user_passwd = $_POST['password'];
        } else {
        	$_user_passwd = md5($_POST['password']);
        }
        $_user_login = $_POST['login'];
        
	    $db->query("select
    	              id,
	                  if ( password = '$_user_passwd', 'Y', 'N' ) as ok
	                from
	                  users
	                where
	                  login = '$_user_login'");
	    $row = $db->fetchAssoc();
	    
	    if ( $row['ok'] == 'Y' ) {
	    	$user_id = $row['id'];
	        @session_register('user_id');
	    }
        unset($row);
        unset($_POST['testjs']);
        unset($_POST['registered']);
        unset($_POST['login']);
        unset($_POST['password']);
        
    } elseif ( isset($_POST['logout']) ) {
    	
        unset($_POST['logout']);
        @session_unregister('user_id');
        @session_destroy();
        
    }
?>
